/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.leon.ui.tree;


import javax.swing.SwingUtilities;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Justin
 */
class DirExpansionListener implements TreeExpansionListener {

    public void treeExpanded(TreeExpansionEvent event) {
        final DefaultMutableTreeNode node = getTreeNode(event.getPath());
        final FileNode fnode = getFileNode(node);
        Thread runner = new Thread() {

            @Override
            public void run() {
                if (fnode != null && fnode.expand(node)) {
                    Runnable runnable = new Runnable() {

                        public void run() {
                            ListJTree.model.reload(node);
                        }
                    };
                    SwingUtilities.invokeLater(runnable);
                }
            }
        };
        runner.start();
    }

    public void treeCollapsed(TreeExpansionEvent event) {
    }

    DefaultMutableTreeNode getTreeNode(TreePath path) {
        return (DefaultMutableTreeNode) (path.getLastPathComponent());
    }

    FileNode getFileNode(DefaultMutableTreeNode node) {
        if (node == null) {
            return null;
        }
        Object obj = node.getUserObject();
        if (obj instanceof IconData) {
            obj = ((IconData) obj).getObject();
        }
        if (obj instanceof FileNode) {
            return (FileNode) obj;
        } else {
            return null;
        }
    }


}
